链表结构
–单向链表–
封装链表类:
function LinkedList() { //内部的类:节点类 function Node(data) { this.data = data this.next = null } //属性 this.head = null this.length = 0 <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 链表 方法:</span><br><span class="line"></span><br><span class="line">* ```javascript</span><br><span class="line"> //1.追加方法</span><br><span class="line"> LinkedList.prototype.append = function (data) {</span><br><span class="line"> //1.创建新的节点</span><br><span class="line"> let newNode = new Node(data)</span><br><span class="line"> //2.判断是否添加到是第一个节点</span><br><span class="line"> if (this.length == 0) { //2.1是第一个节点</span><br><span class="line"> this.head = newNode</span><br><span class="line"> } else { //2.2不是第一个节点</span><br><span class="line"> let newNoad = new Node(data)</span><br><span class="line"> let current = this.head</span><br><span class="line"> while (current.next) {</span><br><span class="line"> current = current.next</span><br><span class="line"> }</span><br><span class="line"> //最后节点的next指向新的节点</span><br><span class="line"> current.next = newNoad</span><br><span class="line"> }</span><br><span class="line"> //3.length+1</span><br><span class="line"> this.length += 1</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
toString方法
//2.toString方法 LinkedList.prototype.toString = function () { //1.定义变量 let current = this.head let listString = " " //2.循环获取一个个的节点 while (current) { listString += current.data + " " current = current.next } return listString } <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* insert方法</span><br><span class="line"></span><br><span class="line">* ```javascript</span><br><span class="line"> LinkedList.prototype.insert = function (position, data) {</span><br><span class="line"> //1).对position进行越界判断</span><br><span class="line"> //假如只有四个元素,传入position为100的话return false</span><br><span class="line"> if (position < 0 || position > this.length) return false</span><br><span class="line"> //2).根据data 创建newNode</span><br><span class="line"> let newNode = new Node(data)</span><br><span class="line"> //3).判断插入的位置是否是第一个</span><br><span class="line"> if (position === 0) {</span><br><span class="line"> newNode.next = this.head</span><br><span class="line"> this.head = newNode</span><br><span class="line"> } else {</span><br><span class="line"> let index = 0</span><br><span class="line"> let current = this.head</span><br><span class="line"> let previous = null</span><br><span class="line"> while (index++ < position) {</span><br><span class="line"> previous = current</span><br><span class="line"> current = current.next</span><br><span class="line"> }</span><br><span class="line"> newNode.next = current</span><br><span class="line"> previous.next = newNode</span><br><span class="line"> }</span><br><span class="line"> //4).length+1</span><br><span class="line"> this.length += 1</span><br><span class="line"> return true</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
get方法
LinkedList.prototype.get = function (position) { //越界判断 if (position < 0 || position >= this.length) return null //获取对应的信息data let current = this.head let index = 0 while (index++ < position) { current = current.next } return current.data } <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* indexOf方法</span><br><span class="line"></span><br><span class="line">* ```</span><br><span class="line"> LinkedList.prototype.indexOf = function (data) {</span><br><span class="line"> //5.1变量定义</span><br><span class="line"> let current = this.head</span><br><span class="line"> let index = 0</span><br><span class="line"> //5.2开始</span><br><span class="line"> while (current) {</span><br><span class="line"> if (current.data === data) {</span><br><span class="line"> return index</span><br><span class="line"> }</span><br><span class="line"> current = current.next</span><br><span class="line"> index++</span><br><span class="line"> }</span><br><span class="line"> return -1</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
update
//6.update方法 LinkedList.prototype.update = function (position, newData) { //1.越界判断 if (position < 0 || position >= this.length) return false //2.查找正确的节点 let current = this.head let index = 0 while (index++ < position) { current = current.next } current.data = newData return true } <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* removeAt方法</span><br><span class="line"></span><br><span class="line">* ```javascript</span><br><span class="line"> LinkedList.prototype.removeAt = function (position) {</span><br><span class="line"> //1).越界判断</span><br><span class="line"> if (position < 0 || position >= this.length) return null</span><br><span class="line"> </span><br><span class="line"> //2).判断是否在第一个节点删除</span><br><span class="line"> let current = this.head</span><br><span class="line"> if (position == 0) {</span><br><span class="line"> this.head = this.head.next</span><br><span class="line"> } else {</span><br><span class="line"> let index = 0</span><br><span class="line"> let previous = null</span><br><span class="line"> while (index++ < position) {</span><br><span class="line"> previous = current</span><br><span class="line"> current = current.next</span><br><span class="line"> }</span><br><span class="line"> previous.next = current.next</span><br><span class="line"> }</span><br><span class="line"> //3)长度减少了一</span><br><span class="line"> this.length -= 1</span><br><span class="line"> </span><br><span class="line"> return current.data</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
remove方法
LinkedList.prototype.remove=function (data) { //1).获取data在列表中的位置 let position=this.indexOf(data) //2).根据位置信息删除节点 return this.removeAt(position) } <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"> 其他:</span><br><span class="line"></span><br><span class="line">* ```javascript</span><br><span class="line"> //9.isEmpty方法</span><br><span class="line"> LinkedList.prototype.isEmpty=function () {</span><br><span class="line"> return this.length==0</span><br><span class="line"> }</span><br><span class="line"> //10.size</span><br><span class="line"> LinkedList.prototype.size=function () {</span><br><span class="line"> return this.length</span><br><span class="line"> }</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
双向链表
1 | //封装双向链表 |